package cc.lj.internal.coupons.domain.martcap.service.impl;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.PageBean;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.martcap.model.vo.MartCapDO;
import cc.lj.internal.coupons.domain.martcap.model.vo.convert.MartCapDOConvertPO;
import cc.lj.internal.coupons.domain.martcap.repository.IMartCapRepository;
import cc.lj.internal.coupons.domain.martcap.service.IMartCapDomainService;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.MartCapPO;
import cc.lj.internal.coupons.interfaces.dto.MartCapDTO;
import cc.lj.internal.coupons.interfaces.dto.query.MartCapListQuery;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.rowset.serial.SerialException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MartCapDomainServiceImpl implements IMartCapDomainService {
    @Resource(name = "martCapRepositoryImpl")
    private IMartCapRepository martCapRepository;

    @Override
    public Integer insert(MartCapDO martCapDO) {
        //截止日期不能大于开始日期
        if(martCapDO.getEndDate().isBefore(martCapDO.getStartDate()))
        {
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_DATE_ERROR);
        }
        MartCapPO martCapPO = MartCapDOConvertPO.INSTANCE.convert(martCapDO);
        return martCapRepository.insert(martCapPO);
    }

    @Override
    public MartCapDO getById(Integer id) {
        MartCapPO martCapPO =  martCapRepository.getById(id);
        return MartCapDOConvertPO.INSTANCE.convert(martCapPO);
    }

    @Override
    public Integer updateById(MartCapDO martCapDO) {
        //截止日期不能大于开始日期
        if(martCapDO.getEndDate().isBefore(martCapDO.getStartDate()))
        {
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_DATE_ERROR);
        }
        //查询当前活动by id
        MartCapPO martCapPO = martCapRepository.getById(martCapDO.getId());
        MartCapDO martCapDOMysql = MartCapDOConvertPO.INSTANCE.convert(martCapPO);
        //活动开始不能修改开始日期
        if(martCapDOMysql.getIsStart()==1 && martCapDO.getStartDate()!=null)
        {
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_MODIFY_STARTTIME);
        }
        //活动已结束，不能修改
        if(martCapDOMysql.getIsOver()==1)
        {
            throw new ServiceException(CoupousExceptionEnum.MART_CAP_IS_OVER);
        }

        Integer row = martCapRepository.updateById(MartCapDOConvertPO.INSTANCE.convert(martCapDO));
        return row;
    }

    @Override
    public PageBean<MartCapDO> getList(PageParam<MartCapDO> pageParam) {
        PageParam<MartCapPO> new_pageParam = new PageParam<>();
        if(pageParam.getParam()!=null)
        {
            MartCapPO martCapPO = MartCapDOConvertPO.INSTANCE.convert(pageParam.getParam());
            new_pageParam.setParam(martCapPO);
        }else
        {
            new_pageParam.setParam(null);
        }
        new_pageParam.setOrderBy(pageParam.getOrderBy());
        new_pageParam.setPageNum(pageParam.getPageNum());
        new_pageParam.setPageSize(pageParam.getPageSize());
        PageInfo<MartCapPO> pageInfo = martCapRepository.selectPage(new_pageParam);

        List<MartCapDO> newList = pageInfo.getList().stream().map(t -> MartCapDOConvertPO.INSTANCE.convert(t)).collect(Collectors.toList());
        PageBean<MartCapDO>  pageBean = PageBean.createPageBean(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getTotal(),newList);
        return pageBean;
    }

    @Override
    public List<MartCapDO> getByIdIn(List<Integer> idList) {
        //通过主键 in 查询 封顶额活动表数据
        List<MartCapPO> martCapPOList = martCapRepository.getByIdIn(idList);
        return martCapPOList.stream().map( x -> MartCapDOConvertPO.INSTANCE.convert(x)).collect(Collectors.toList());
    }


}
